home *** CD-ROM | disk | FTP | other *** search
/ Ray Dream Studio 5 / Ray Dream.iso / pc / DreamSDK / Windows / SAMPLES / PRIMITIV / STAR / COMSTAR.CPP next >
Encoding:
C/C++ Source or Header  |  1997-07-11  |  8.3 KB  |  273 lines

  1. // Copyright (c)1995 Ray Dream, Inc. All Rights Reserved.
  2. /* $Id: COMStar.cpp 1.13 1997/06/20 22:34:19 damien Exp $ */
  3.  
  4. ////////////////////////////////////////////////////////////////////////
  5. //   Geometric Primitive Example : Facets                             //
  6. //--------------------------------------------------------------------//
  7. //   Implementation of the Facets Interface                           //
  8. //////////////////////////////////////////////////////////////////////// 
  9.  
  10. #include "math.h"
  11.  
  12. #ifndef __COMSTAR__
  13. #include "COMSTAR.h"
  14. #endif
  15.  
  16. #ifndef __I3DSHUTI__
  17. #include "I3DShUti.h"
  18. #endif 
  19.  
  20. #ifndef __ISHFMESH__
  21. #include "IShFMesh.h"
  22. #endif 
  23.  
  24. #ifndef __3DCOFAIL__
  25. #include "3DCoFail.h"
  26. #endif
  27.          
  28. // Constructor / Destructor of the C++ Object :
  29. Facets::Facets() {
  30.   fCRef=0; // Reference Counter
  31.   fData.fNbBranches=5;
  32.   }
  33.   
  34. Facets::~Facets() {
  35.   global_count_Obj--; 
  36.   }
  37.   
  38. // IUnknown Interface :
  39. HRESULT Facets::QueryInterface(THIS_ REFIID riid,LPVOID FAR* ppvObj) {
  40.   *ppvObj=NULL;
  41.   
  42.   // The Facets knows the interfaces of the parent Objects
  43.   if (IsEqualIID(riid, IID_IUnknown))
  44.     *ppvObj=(IUnknown*)(I3DExGeometricPrimitive*)this;
  45.   else if (IsEqualIID(riid, IID_I3DExGeometricPrimitive))
  46.     *ppvObj=(I3DExGeometricPrimitive*)this;
  47.   else if (IsEqualIID(riid, IID_I3DExDataExchanger))
  48.     *ppvObj=(I3DExDataExchanger*)this;
  49.   else if (IsEqualIID(riid, IID_I3DExtension))
  50.     *ppvObj=(I3DExtension*)this;
  51.     
  52.   // we must add reference if we return an interface
  53.   if (*ppvObj!=NULL) {
  54.     ((LPUNKNOWN)*ppvObj)->AddRef();
  55.     return NOERROR;
  56.     }
  57.   else {
  58.     return ResultFromScode(E_NOINTERFACE);
  59.     }
  60.   }
  61.  
  62. ULONG Facets::AddRef() {
  63.   return this->fCRef++;
  64.   }
  65.   
  66. ULONG Facets::Release() {
  67.   ULONG UnreleaseObject=fCRef--;
  68.   
  69.   if (fCRef==0)
  70.      delete this; // No reference left, so destroy the object
  71.   
  72.   return UnreleaseObject;
  73.   // local variable used, because fCRef can be destroyed before.
  74.   }
  75.   
  76. // I3DExtension methods :
  77. I3DExtension* Facets::Clone() {
  78.   Facets* theClone = new Facets;
  79.   if (theClone) {
  80.     theClone->AddRef();
  81.     theClone->fData=fData;
  82.     }                               
  83.   return (I3DExtension*)theClone;
  84.   }   
  85.  
  86. HRESULT Facets::ShellUtilitiesInit(THIS_ IShUtilities* shellUtilities) {
  87.   return NOERROR;
  88.   }
  89.  
  90. // I3DExDataExchanger methods :
  91. ExtensionDataMap* Facets::GetExtensionDataMap() {
  92.   return NULL;
  93.   }               
  94.   
  95. void* Facets::GetExtensionDataBuffer() {
  96.   return &fData;
  97.   }
  98.   
  99. HRESULT Facets::ExtensionDataChanged() {
  100.   return NOERROR;
  101.   }
  102.  
  103. HRESULT Facets::HandleEvent(THIS_ ULONG SourceID) {
  104.   return ResultFromScode(E_NOTIMPL);
  105.   }
  106.  
  107. short Facets::GetResID() {
  108.   return 136; // always return -1, if you do not have a view
  109.               // Here, we have a view to change the number of branches of the Star
  110.   }
  111.   
  112. // I3DExGeometricPrimitive methods
  113. // -- Geometry calls
  114. HRESULT Facets::EnumPatches(THIS_ EnumPatchesCallback callback, void* privData) {
  115.   return ResultFromScode(E_NOTIMPL);
  116.     }
  117.   
  118. HRESULT Facets::EnumFacets(THIS_ EnumFacetsCallback callback, void* privData, NUM3D fidelity) {
  119.   short i, j;
  120.   FACET3D StarFacet;
  121.   VECTOR3D v1,v2,normal;
  122.   NUM3D angle,anglestep,radius1,radius2,radiusswap,sinus,cosinus;
  123.   NUM3D k360=3.1415926535897932384626233 * 2;
  124.     
  125.   angle=0.0;
  126.   anglestep=k360/((NUM3D)fData.fNbBranches)/2;
  127.   radius1=10.0;
  128.   radius2=4.0;
  129.     
  130.   // Inferior Facets
  131.   // -- Common Vertex of each inferior facets
  132.   StarFacet.fVertices[0].fVertex[0]=0.0;
  133.   StarFacet.fVertices[0].fVertex[1]=0.0;
  134.     
  135.   for (i=0;i<(fData.fNbBranches*2);i++) { 
  136.     // Variable information of the common vertex                     
  137.     StarFacet.fUVSpace=0;
  138.     StarFacet.fVertices[0].fUV[1]=(((float)i)+0.5)/((float)fData.fNbBranches*2);
  139.         //(angle + anglestep/2.0)/k360;
  140.     // Second Vertex information
  141.     sinus=sin(angle);
  142.         cosinus=cos(angle); //QuickSinCos(angle,sinus,cosinus);
  143.     StarFacet.fVertices[1].fVertex[0]=cosinus*radius1;
  144.     StarFacet.fVertices[1].fVertex[1]=sinus*radius1;
  145.     StarFacet.fVertices[1].fVertex[2]=0.0;
  146.     StarFacet.fVertices[1].fUV[0]=0.5;
  147.     StarFacet.fVertices[1].fUV[1]=((float)i)/((float)fData.fNbBranches*2); //angle/k360;
  148.     // Last Vertex information
  149.     angle += anglestep;
  150.     sinus=sin(angle);
  151.         cosinus=cos(angle); //QuickSinCos(angle,sinus,cosinus);
  152.     StarFacet.fVertices[2].fVertex[0]=cosinus*radius2;
  153.     StarFacet.fVertices[2].fVertex[1]=sinus*radius2;
  154.     StarFacet.fVertices[2].fVertex[2]=0.0;
  155.     StarFacet.fVertices[2].fUV[0]=0.5;
  156.     StarFacet.fVertices[2].fUV[1]=angle/k360;
  157.     // Swap the radius to "alternate" the point
  158.     radiusswap=radius1;radius1=radius2;radius2=radiusswap;
  159.     // Normal vector calculation
  160.         for (j=0; j<3; j++) {
  161.             v1[j]=StarFacet.fVertices[1].fVertex[j]-StarFacet.fVertices[0].fVertex[j];
  162.             v2[j]=StarFacet.fVertices[2].fVertex[j]-StarFacet.fVertices[0].fVertex[j];
  163.             normal[j]=v2[(j+1)%3]*v1[(j+2)%3] - v2[(j+2)%3]*v1[(j+1)%3];
  164.             }
  165.     /*normal[0]=v2[1]*v1[2] - v2[2]*v1[1];
  166.     normal[1]=v2[2]*v1[0] - v2[0]*v1[2];
  167.     normal[2]=v2[0]*v1[1] - v2[1]*v1[0]; // Vectorial product*/
  168.         NUM3D norm=sqrt(SQR(normal[0]) + SQR(normal[1]) + SQR(normal[2]));
  169.         normal[0] /= norm;
  170.         normal[1] /= norm;
  171.         normal[2] /= norm; // Normalization
  172.         for (j=0; j<3; j++) {
  173.             StarFacet.fVertices[0].fNormal[j]=normal[j]; // the 3 points have the same normal vector
  174.             StarFacet.fVertices[1].fNormal[j]=normal[j];
  175.             StarFacet.fVertices[2].fNormal[j]=normal[j];
  176.             }
  177.     // callback is used to give a facet to the shell
  178.       // Inferior Facets
  179.       StarFacet.fVertices[0].fVertex[2]=-4.0;
  180.     StarFacet.fVertices[0].fUV[0]=1.0;
  181.     callback(&StarFacet,privData);          
  182.       // Superior Facets
  183.         for (j=0; j<3; j++) {
  184.             // reverse normals
  185.             StarFacet.fVertices[j].fNormal[2]=-StarFacet.fVertices[j].fNormal[2];
  186.             }
  187.       StarFacet.fVertices[0].fVertex[2]=4.0;
  188.     StarFacet.fVertices[0].fUV[0]=0.0; // changed
  189.     callback(&StarFacet,privData);          
  190.         }         
  191.     
  192.   return NOERROR;
  193.     }              
  194.     
  195. void AccuFacet(FACET3D* facet, void* accu) {
  196.     ((IShFacetMeshAccumulator*)accu)->AddFacet(facet);
  197.     }
  198.  
  199. HRESULT Facets::GetNbrLOD(short &nbrLod) {
  200.   return ResultFromScode(E_NOTIMPL);
  201.     }
  202.  
  203. HRESULT Facets::GetLOD(short lodIndex, NUM3D &lod) {
  204.   return ResultFromScode(E_NOTIMPL);
  205.     }
  206.  
  207. HRESULT Facets::MakeFacetMesh(short index, FacetMesh &amesh) {
  208.     IShFacetMeshAccumulator* accu;
  209.     gShellUtilities->CoCreateInstance(CLSID_StandardFacetMeshAccumulator, NULL, CLSCTX_INPROC_SERVER, IID_IShFacetMeshAccumulator, (LPVOID*)&accu);
  210.     EnumFacets(AccuFacet, accu, 0);
  211.     accu->MakeFacetMesh(amesh);
  212.     accu->Release();
  213.     return S_OK;
  214.     }
  215.                         
  216. HRESULT Facets::MakeFacetMesh(NUM3D lod, FacetMesh &amesh) {
  217.     return MakeFacetMesh((short)0, amesh);
  218.     }
  219.  
  220. // Give the boundary Box
  221. HRESULT Facets::GetBBox(THIS_ BOX3D* bbox) {
  222.     bbox->fMin[0]=-10.0;
  223.   bbox->fMax[0]=10.0;
  224.   bbox->fMin[1]=-10.0;
  225.   bbox->fMax[1]=10.0;
  226.   bbox->fMin[2]=-4.0;
  227.   bbox->fMax[2]=4.0;
  228.   return NOERROR;
  229. }
  230.   
  231. // -- Shading calls
  232. ULONG Facets::GetUVSpaceCount() 
  233. {
  234.   return 1; // the star is describe with only 1 UV-Space
  235. }
  236.  
  237. HRESULT Facets::GetUVSpace(THIS_ ULONG uvSpaceID, UVSpaceInfo* uvSpaceInfo) 
  238. {
  239.   if (uvSpaceID == 0)
  240.   { 
  241.     uvSpaceInfo->fMin[0] = 0.0; // u coordinate goes from 0 to 1
  242.     uvSpaceInfo->fMax[0] = 1.0;
  243.     uvSpaceInfo->fMin[1] = 0.0; // v coordinate goes from 0 to 1
  244.     uvSpaceInfo->fMax[1] = 1.0;
  245.     uvSpaceInfo->fWraparound[0] = FALSE;  // No Wrap around
  246.     uvSpaceInfo->fWraparound[1] = FALSE;
  247.     // uvSpaceInfo->fIsFlatSurface = FALSE;  // the surface is not flat
  248.   }
  249.   return NOERROR;
  250. }
  251.   
  252. // We use the default interpolation method to get all the coordinate of a point in UV Coordinates
  253. HRESULT Facets::UV2XYZ(THIS_ VECTOR2D* uv, ULONG uvSpaceID, VECTOR3D* resultPosition, BOOLEAN* inUVSpace) {
  254.   return ResultFromScode(E_NOTIMPL);
  255. }
  256.  
  257. // -- Ray Tracing calls
  258. HRESULT Facets::RayHit(THIS_ BOOLEAN* didHit, Ray3D* aR, RayHitParameters* RayHitParams, RayHit3D* hit) {
  259.   return ResultFromScode(E_NOTIMPL);
  260.   }
  261.   
  262. HRESULT Facets::GetRayHitDetails(THIS_ RayHit3D* hit) {
  263.   return ResultFromScode(E_NOTIMPL);
  264.   }
  265.   
  266. HRESULT Facets::RayAllHits(THIS_ Ray3D* aR, NUM3D tmin, NUM3D tmax, RayHit3D* hit, RayHitCallback callback, void* privData) {
  267.   return ResultFromScode(E_NOTIMPL);
  268.   }
  269.   
  270.  
  271.  
  272.  
  273.